﻿using DataChangeTools.Utils.Constants;
using GeneralUpdate.ClientCore;
using GeneralUpdate.Common.Download;
using GeneralUpdate.Common.Internal;
using GeneralUpdate.Common.Internal.Bootstrap;
using GeneralUpdate.Common.Shared.Object;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DataChangeTools.Utils
{
    public class AutoUpdaterHelper
    {
        private Configinfo configinfo;

        #region 事件
        // 创建事件
        public event Action<object, MultiDownloadCompletedEventArgs> MultiDownloadCompletedAction;
        public event Action<object, MultiDownloadStatisticsEventArgs> MultiDownloadStatisticsAction;
        public event Action<object, MultiAllDownloadCompletedEventArgs> OnMultiAllDownloadCompletedAction;
        public event Action<object, MultiDownloadErrorEventArgs> OnMultiDownloadErrorAction;
        public event Action<object, GeneralUpdate.Common.Internal.ExceptionEventArgs> OnExceptionAction;

        #endregion


        public AutoUpdaterHelper()
        {

            configinfo = new Configinfo
            {
                //configinfo.UpdateLogUrl = "https://www.baidu.com";
                ReportUrl = "http://127.0.0.1:5000/Upgrade/Report",
                UpdateUrl = "http://127.0.0.1:5000/Upgrade/Verification",
                AppName = "Updater/UpgradeSample.exe",
                MainAppName = "ClientSample.exe",
                InstallPath = Thread.GetDomain().BaseDirectory,
                //configinfo.Bowl = "Generalupdate.CatBowl.exe";
                //当前客户端的版本号
                ClientVersion = "1.0.0.0",
                //当前升级端的版本号
                UpgradeClientVersion = "1.0.0.0",
                //产品id
                ProductId = "2d974e2a-31e6-4887-9bb1-b4689e98c77a",
                //应用密钥
                AppSecretKey = "dfeb5833-975e-4afb-88f1-6278ee9aeff6",
                //BlackFiles = new List<string> { "123.exe" },
                //BlackFormats = new List<string> { "123.dll" },
                //BlackDirectories = new List<string> { "123" },
                //Scheme = "Bearer",
                //Token = "..."
            };
        }
        public AutoUpdaterHelper(Configinfo config)
        {
            configinfo = config;
        }

        public AutoUpdaterHelper(string BaseUrl, string UpgradeClientVersion)
        {

            Assembly assembly = Assembly.GetEntryAssembly();
            Version version = assembly.GetName().Version;

            configinfo = new Configinfo
            {
                //UpdateLogUrl = "https://www.baidu.com";
                ReportUrl = $"{BaseUrl}{AutoUpdaterConstants.ReportUrl}",
                UpdateUrl = $"{BaseUrl}{AutoUpdaterConstants.UpdateUrl}",
                AppName = "Updater/DataChangeTools.Updater.exe",
                MainAppName = "DataChangeTools.exe",
                InstallPath = Thread.GetDomain().BaseDirectory,
                //configinfo.Bowl = "Generalupdate.CatBowl.exe";
                //当前客户端的版本号
                ClientVersion = version.ToString(),
                //当前升级端的版本号
                UpgradeClientVersion = version.ToString(),
                //产品id
                ProductId = AutoUpdaterConstants.ProductId,
                //应用密钥
                AppSecretKey = AutoUpdaterConstants.AppSecretKey,
                //BlackFiles = new List<string> { "123.exe" },
                //BlackFormats = new List<string> { "123.dll" },
                //BlackDirectories = new List<string> { "123" },
                //Scheme = "Bearer",
                //Token = "..."
            };
        }

        public async void Update()
        {
            _ = await new GeneralClientBootstrap()
              //单个或多个更新包下载通知事件
              //单个或多个更新包下载速度、剩余下载事件、当前下载版本信息通知事件
              .AddListenerMultiDownloadStatistics(OnMultiDownloadStatistics)
              //单个或多个更新包下载完成
              .AddListenerMultiDownloadCompleted(OnMultiDownloadCompleted)
              //完成所有的下载任务通知
              .AddListenerMultiAllDownloadCompleted(OnMultiAllDownloadCompleted)
              //下载过程出现的异常通知
              .AddListenerMultiDownloadError(OnMultiDownloadError)
              //整个更新过程出现的任何问题都会通过这个事件通知
              .AddListenerException(OnException)
              .SetConfig(configinfo)
              .Option(UpdateOption.DownloadTimeOut, 60)
              .Option(UpdateOption.Encoding, Encoding.Default)
              .LaunchAsync();
            Console.WriteLine($"主程序已启动，{DateTime.Now}！");
            await Task.Delay(2000);
        }


        #region 事件方法实现


        #region  升级方式实现


        void OnMultiDownloadError(object arg1, MultiDownloadErrorEventArgs arg2)
        {
            OnMultiDownloadErrorAction?.Invoke(this, arg2);
        }

        void OnMultiAllDownloadCompleted(object arg1, MultiAllDownloadCompletedEventArgs arg2)
        {
            OnMultiAllDownloadCompletedAction?.Invoke(this, arg2);
        }

        void OnMultiDownloadCompleted(object arg1, MultiDownloadCompletedEventArgs arg2)
        {
            MultiDownloadCompletedAction?.Invoke(this, arg2);
        }

        void OnMultiDownloadStatistics(object arg1, MultiDownloadStatisticsEventArgs arg2)
        {
            MultiDownloadStatisticsAction?.Invoke(this, arg2);
        }

        void OnException(object arg1, ExceptionEventArgs arg2)
        {
            OnExceptionAction?.Invoke(this, arg2);
        }

        #endregion

        #endregion


    }
}
